<template>
    <List v-if="true" :list="momentList" custom-class="momentsWrapper" @customScroll="handleScroll" @loadData="getMomentListHandler" :finished="finished">
      <template #top>
        <div class="headerBtn" :class="{ fix: fix, noDrag: settingStore.fixWindow }">
            <div>
              <div class="opIcon"><SoundOutlined /></div>
              <div class="opIcon"><ReloadOutlined @click="reloadMoments"/></div>
            </div>
            <div>
              <div class="opIcon" :class="{fixActive: settingStore.fixWindow}" @click="fixWindow"><AimOutlined /></div>
              <div class="opIcon" @click="minimize"><LineOutlined /></div>
              <div class="opIcon close" @click="closeMomentsWin"><CloseOutlined /></div>
            </div>
        </div>
        <!--背景图-->
        <div class="top">
          <div class="bg">
            <img :src="momentInfo.bgImg" ref="bgRef" />
          </div>
          <div class="fixMenu"></div>
          <div class="userInfo">
            <span>你的头发乱了噢</span>
            <img src="@/assets/images/tx.jpg" />
          </div>
        </div>
      </template>
      <template #item="{item}">
        <MomentItem :key="item.id" :moment="item" />
      </template>
    </List>
</template>
<script lang="ts" setup>
//------------- 导入工具库 -------------//
import { ref, onMounted, onUnmounted, nextTick } from 'vue'
import PubSub from 'pubsub-js'
import {getMomentList} from '@/lib/api.ts'
import { closeMomentsWin } from '@/utils/Electron'
//------------- 导入组件库 -------------//
import {
  AimOutlined,
  SoundOutlined,
  ReloadOutlined,
  LineOutlined,
  ExpandOutlined,
  CloseOutlined
} from '@ant-design/icons-vue'
import List from '@/components/custom/List/index.vue'
import MomentItem from '@/components/moments/MomentItem.vue'
import useAuthStore from '@/stores/authStore'
import { ipcRenderer } from '@/utils/Electron.ts'
import useSettingStore from '@/stores/settingStore'
//------------- 定义属性 -------------//
const settingStore = useSettingStore()
const authStore = useAuthStore()
const bgRef = ref(null)
const fix = ref<Boolean>(false)
let observer = ref<IntersectionObserver>()
const momentList = ref<Array<any>>([])
const maxCounts = 20
const finished = ref<Boolean>(false)
const momentInfo = authStore.momentInfo
//------------- 定义方法 -------------//
const reloadMoments = () => {
  momentList.value = []
  getMomentListHandler()
}
// 获取朋友圈列表
const getMomentListHandler = (start = momentList.value.length, maxCount = maxCounts) => {
  getMomentList(start, maxCount).then((res: any)=> {
    if(res.code==0) {
      finished.value = !res.data.next
      momentList.value = [...momentList.value, ...res.data.list]
    }
  })
}
const setupIntersectionObserver = () => {
  observer.value = new IntersectionObserver(
    (entries) => {
      const entry = entries[0]
      if (entry.isIntersecting) {
        // 出现在视窗之内
        fix.value = false
      } else {
        // 消失在视窗
        fix.value = true
      }
    },
    {
      root: null, //默认为视窗
      rootMargin: '0px',
      threshold: [0, 1] // 当元素0%或100%可见时触发回调
    }
  )
  observer.value.observe(bgRef.value)
}

// 检测鼠标滚动事件
const handleScroll = () => {
  PubSub.publish('momentScroll', {}) // 注意这里我修改了事件名为 'cancelRightMenu'，因为 'canncel' 可能是个拼写错误
}
// 固定窗口
const fixWindow = () => {
  settingStore.setFixWindow()
}
// 最小化
const minimize = () => {
  ipcRenderer().invoke('sent-event', {
    type: 'minimize-window:moment'
  })
}
//------------- 生命周期 -------------//
onMounted(() => {
  console.log(`onMouted: ${JSON.stringify(useAuthStore().momentInfo)}`)
  nextTick(()=> {
    setupIntersectionObserver()
  })
  getMomentListHandler()
})
onUnmounted(()=> {
  observer.value?.disconnect()
})
</script>
<style lang="less" scoped>
@import '@/assets/base.less';
.momentsWrapper {
  height: 800px;
  width: 100%;
  padding-bottom: 40px;
  position: relative;
  -webkit-app-region: no-drag;
  overflow: scroll;
  background-color: white;
  .headerBtn {
    -webkit-app-region: drag;
    display: flex;
    justify-content: space-between;
    width: 100%;
    height: 35px;
    position: fixed;
    color: #000;
    z-index: 9;
    align-items: center;
    font-size: 14px;
    color: white;
    &.noDrag {
      -webkit-app-region: no-drag;
    }
    &.fix {
      background-color: #e0e0e0;
      color: #7d7d7d;
      .opIcon:hover {
        background-color: #d1d1d1;
      }
    }
    div {
      height: 100%;
      align-items: center;
      -webkit-app-region: no-drag;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .opIcon {
        &:hover {
          background-color: #3838383d;
        }
        &.close:hover {
          background-color: rgba(255, 0, 0, 0.562) !important;
          color: #000;
        }
        &.fixActive {
          background-color: rgba(255, 0, 0, 0.562) !important;
        }
        display: flex;
        align-items: center;
        justify-content: center;
        width: 40px;
        height: 100%;
      }
    }
  }
  .top {
    width: 100%;
    display: flex;
    justify-content: center;
    position: relative;
    margin-bottom: 10px;
    height: 350px;
    .bg {
      width: 100%;
      height: 352px;
      overflow: hidden;
      display: flex;
      align-items: flex-start;
      justify-content: center;
      height: 325px;
      img {
        width: 100%;
        height: 328px;
        object-fit: cover;
      }
    }
  }

  .userInfo {
    position: absolute;
    bottom: 0;
    width: 100%;
    display: flex;
    justify-content: flex-end;
    padding-right: 5%;
    gap: 13px;
    align-items: flex-start;
    span {
      color: white;
      margin-top: 5px;
      font-size: 15.5px;
    }
    img {
      width: 60px;
      height: 60px;
    }
  }
  .momentList {
    margin-top: 25px;
  }
}
</style>
